Test Series - java script

Test Number 9/92

Q: What is the observation made in the following JavaScript code?

var count = [1,,3];
A. The omitted value takes “undefined”
B. This results in an error
C. This results in an exception
D. The omitted value takes an integer value
Solution: Array is defined with a null value when no value is mentioned. If you omit a value from an array literal, the omitted element is given an undefined value.
Q: The pop() method of the array does which of the following task?
A. decrements the total length by 1
B. increments the total length by 1
C. prints the first element but no effect on the length
D. updates the element
Solution: pop() function pops out that is delete the last element from the array. Hence pop() method (it works with push()) reduces the length of an array by 1.
Q: What is the observation made in the following JavaScript code?

if (!a[i]) continue;
A. Skips the defined elements
B. Skips the existent elements
C. Skips the null elements
D. Skips the defined & existent elements
Solution: The if loop in the above code checks whether the value of a[i] exists or not. For undefined, non existent and null values the if loop returns true.
Q: What will happen if reverse() and join() methods are used simultaneously?
A. Reverses and stores in the same array
B. Reverses and concatenates the elements of the array
C. Reverses
D. Stores the elements of an array in normal order
Solution: The array.join() method is an inbuilt function in JavaScript which is used to join the elements of an array into a string. The reverse() followed by a join() will reverse the respective array and will store the reversed array in the memory.
Q: What will be the possible output of the following JavaScript code?

var a = [1,2,3,4,5];
a.slice(0,3);
A. Returns [1,2,3]
B. Returns [4,5]
C. Returns [1,2,3,4]
D. Returns [1,2,3,4,5]
Solution: slice() function is a predefined function in JavaScript used to keep the elements from starting point and ending point mentioned in the function argument. The elements after the ending point and before the starting point are not shown.
Q: The primary purpose of the array map() function is that it __________
A. maps the elements of another array into itself
B. passes each element of the array and returns the necessary mapped elements
C. passes each element of the array on which it is invoked to the function you specify, and returns an array containing the values returned by that function
D. pass the elements of the array into another array
Solution: map() is a predefined function in javascript used for mapping the array elements to be used for some other purpose. The map() method passes each element of the array on which it is invoked to the function you specify, and returns an array containing the values returned by that function.
Q: The reduce and reduceRight methods follow a common operation called __________
A. filter and fold
B. inject and fold
C. finger and fold
D. fold
Solution: The reduceRight() method reduces the array to a single value. The reduceRight() method executes a provided function for each value of the array (from right-to-left). The return value of the function is stored in an accumulator (result/total). Hence it does the operation of injecting and folding.
Q: The method or operator used to identify the array is __________
A. isarrayType()
B. ==
C. ===
D. typeof
Solution: The typeof operator is used to get the data type (returns a string) of its operand. The operand can be either a literal or a data structure such as a variable, a function, or an object. The operator returns the data type.
Q: What will be the output of the following JavaScript code?

var arr=[1,2,3];  
var rev=arr.reverse();  
document.writeln(rev);
A. 1, 2, 3
B. 3, 2, 1
C. 3
D. 1
Solution: reverse function is a predefined function in array library in Javascript. The function is used to reverse the element of the array.
i.e. 3, 2, 1.
Q: What will be the output of the following JavaScript code?

var values=["one","two","Three"];  
var ans=values.shift();  
document.writeln(ans);
A. one
B. two
C. three
D. error
Solution: shift is a predefined function in array library and works like a pop function. It pops the first value of the array and returns its value. Therefore the answer will be one.

You Have Score    /10